package com.vector.update_app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.vector.update_app.listener.ExceptionHandler;
import com.vector.update_app.listener.IUpdateDialogFragmentListener;
import com.vector.update_app.service.DownloadService;
import com.vector.update_app.utils.AppUpdateUtils;
import com.vector.update_app.view.CProgressDialogUtils;
import com.vector.update_app.view.HProgressDialogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class DialogUtils {

    /**
     * 自定义对话框
     *
     * @param updateApp
     * @param updateAppManager
     */
    public static void showDiyDialog(final Activity mContext, final UpdateAppBean updateApp, final UpdateAppManager updateAppManager) {
        String targetSize = updateApp.getTargetSize();
        String updateLog = updateApp.getUpdateLog();

        String msg = "";

        if (!TextUtils.isEmpty(targetSize)) {
            msg = "新版本大小：" + targetSize + "\n\n";
        }

        if (!TextUtils.isEmpty(updateLog)) {
            msg += updateLog;
        }

        new AlertDialog.Builder(mContext)
                .setTitle(String.format("是否升级到%s版本？", updateApp.getNewVersion()))
                .setMessage(msg)
                .setPositiveButton("升级", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //显示下载进度
//                        if (isShowDownloadProgress) {
                                updateAppManager.download(new DownloadService.DownloadCallback() {
                                    @Override
                                    public void onStart() {
                                        HProgressDialogUtils.showHorizontalProgressDialog(mContext, "下载进度", false);
                                    }

                                    /**
                                     * 进度
                                     *
                                     * @param progress  进度 0.00 -1.00 ，总大小
                                     * @param totalSize 总大小 单位B
                                     */
                                    @Override
                                    public void onProgress(float progress, long totalSize) {
                                        HProgressDialogUtils.setProgress(Math.round(progress * 100));
                                    }

                                    /**
                                     *
                                     * @param total 总大小 单位B
                                     */
                                    @Override
                                    public void setMax(long total) {

                                    }


                                    @Override
                                    public boolean onFinish(File file) {
                                        HProgressDialogUtils.cancel();
                                        return true;
                                    }

                                    @Override
                                    public void onError(String msg) {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                                        HProgressDialogUtils.cancel();

                                    }

                                    @Override
                                    public boolean onInstallAppAndAppOnForeground(File file) {
                                        return false;
                                    }
                                });
//                        } else {
//                            //不显示下载进度
//                            updateAppManager.download();
//                        }
                                dialog.dismiss();
                            }
                        }
                )
                .setNegativeButton("暂不升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create()
                .show();
    }

    /**
     * 自定义对话框
     *
     * @param updateAppManager
     */
    public static void showDiyDialog(final Activity mContext,String updateLog ,String newVersion, final UpdateAppManager updateAppManager) {
        String targetSize ="12M";

        String msg = "";

        if (!TextUtils.isEmpty(targetSize)) {
            msg = "新版本大小：" + targetSize + "\n\n";
        }

        if (!TextUtils.isEmpty(updateLog)) {
            msg += updateLog;
        }

        new AlertDialog.Builder(mContext)
                .setTitle(String.format("是否升级到%s版本？",newVersion))
                .setMessage(msg)
                .setPositiveButton("升级", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //显示下载进度
//                        if (isShowDownloadProgress) {
                                updateAppManager.download(new DownloadService.DownloadCallback() {
                                    @Override
                                    public void onStart() {
                                        HProgressDialogUtils.showHorizontalProgressDialog(mContext, "下载进度", false);
                                    }

                                    /**
                                     * 进度
                                     *
                                     * @param progress  进度 0.00 -1.00 ，总大小
                                     * @param totalSize 总大小 单位B
                                     */
                                    @Override
                                    public void onProgress(float progress, long totalSize) {
                                        HProgressDialogUtils.setProgress(Math.round(progress * 100));
                                    }

                                    /**
                                     *
                                     * @param total 总大小 单位B
                                     */
                                    @Override
                                    public void setMax(long total) {

                                    }


                                    @Override
                                    public boolean onFinish(File file) {
                                        HProgressDialogUtils.cancel();
                                        return true;
                                    }

                                    @Override
                                    public void onError(String msg) {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                                        HProgressDialogUtils.cancel();

                                    }

                                    @Override
                                    public boolean onInstallAppAndAppOnForeground(File file) {
                                        return false;
                                    }
                                });
//                        } else {
//                            //不显示下载进度
//                            updateAppManager.download();
//                        }
                                dialog.dismiss();
                            }
                        }
                )
                .setNegativeButton("暂不升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create()
                .show();
    }

}
